﻿/*
 * jquery.scrollContent - jquery content scroll plugins
 *
 * Copyright (c) 2008 Wolfyan (server.co.jp@gmail.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 15:28 2008/10/21 $
 * $Rev: 0.1 $
 */
(function (jQuery){
    this.speed = 10;
    this.timeout;
	this.stopScroll = false;      	//标志当前是否停止滚动
	this.isSmoothScroll = true;   	//是否平滑连续滚动
    this.lineHeight = 20;			//默认高度
    this.btnNext;
    this.btnPrev;
    this.scrollContent;
	
    this.inits = function(content,autoStart,timeout,isSmoothScroll){
      this.timeout = timeout;
      this.isSmoothScroll = isSmoothScroll;
      this.scrollContent = $("#"+content  + " #breakList");
      this.btnNext = $("#"+content + " #btnNext");
      this.btnPrev = $("#"+content + " #btnPrev");
      this.scrollContent.append(this.scrollContent.html());
      
      var obj = this;
      this.btnPrev.bind("click",  function(event){ obj.prev(); });
      this.btnNext.bind("click",  function(event){ obj.next(); });
      this.btnPrev.bind("mouseover",  function(event){ obj.mouseOver(); });
      this.btnPrev.bind("mouseout",  function(event){ obj.mouseOut(); });
      this.btnNext.bind("mouseover",  function(event){ obj.mouseOver(); });
      this.btnNext.bind("mouseout",  function(event){ obj.mouseOut(); });
      this.scrollContent.bind("mouseover",  function(event){ obj.mouseOver(); });
      this.scrollContent.bind("mouseout",  function(event){ obj.mouseOut(); });
      
      if(autoStart) { this.start(); }
    };
    this.start = function(){
      if(this.isSmoothScroll){
        this.autoScrollTimer = setInterval('this.smoothScroll()', this.timeout);
      } else {  
        this.autoScrollTimer = setInterval('this.autoScroll()', this.timeout);
      }
    };
    this.prev = function() {
      this.stopScroll = true;
      this.scroll("up");
    };
    this.next = function() {
      this.stopScroll = true;
      this.scroll("down");
    };
    this.stop = function() {
      clearTimeout(this.autoScrollTimer);
      this.DelayTimerStop = 0;
    };
    this.mouseOver = function(){ 
      this.stopScroll = true;
    };
    this.mouseOut = function() {
      this.stopScroll = false;
    };
    this.autoScroll = function() {
      if(this.stopScroll) { return; }
      var top = this.scrollContent.attr("scrollTop");
      this.scrollContent.attr("scrollTop", top+1);
      if(parseInt(this.scrollContent.attr("scrollTop")) % this.lineHeight != 0) {
        this.scrollTimer = setTimeout('this.autoScroll()', this.speed);
      } else {
        if(parseInt(this.scrollContent.attr("scrollTop")) >= parseInt(this.scrollContent.attr("scrollHeight")) / 2) {
          this.scrollContent.attr("scrollTop", 0);
        }
        clearTimeout('this.scrollTimer()');
      }
    };
    this.smoothScroll = function() {
      if(this.stopScroll) { return; }
      var top = this.scrollContent.attr("scrollTop");
      this.scrollContent.attr("scrollTop", top+1);
      if(parseInt(this.scrollContent.attr("scrollTop")) >= parseInt(this.scrollContent.attr("scrollHeight")) / 2) {
        this.scrollContent.attr("scrollTop", 0);
      }
    };
    this.scroll = function(direction) {
      var top = this.scrollContent.attr("scrollTop");
      if(direction=="up") {
        this.scrollContent.attr("scrollTop", top-1);
      } else {
        this.scrollContent.attr("scrollTop", top+1);
      }
      if(parseInt(this.scrollContent.attr("scrollTop")) >= parseInt(this.scrollContent.attr("scrollHeight")) / 2) {
        this.scrollContent.attr("scrollTop", 0);
      } else if (parseInt(this.scrollContent.attr("scrollTop"))<=0) {
        this.scrollContent.attr("scrollTop", parseInt(this.scrollContent.attr("scrollHeight")) / 2);
      }
      if(parseInt(this.scrollContent.attr("scrollTop")) % this.lineHeight != 0) {
        this.scrollTimer = setTimeout('this.scroll("' + direction + '")', this.speed);
      }
    };
    jQuery.scrollContent = this;
    return jQuery;
})(jQuery);